package com.silp.henxui;

import java.util.Random;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources.Theme;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class HenXuiActivity extends Activity{
    protected static final String LOG_TAG = "SILP";
	protected static final String END_CLASS = "Data";
	public int count,num=0;

	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Button themdata = (Button) findViewById(R.id.btn_themdata);
        Button tinhtoan = (Button) findViewById(R.id.btn_tinhtoan);
        Button clear = (Button) findViewById(R.id.btn_clear);
        themdata.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				
				Log.i(LOG_TAG, "Them data on Click");
				createcustomdialog().show();
				
				
			}
		});
        tinhtoan.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View v) {
				int int_result = tinhtoan(count);
				String str_result = laydulieu(int_result);
				Toast.makeText(getApplicationContext(), str_result, 100).show();
				
			}
		});
        
        clear.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				cleardulieu();
				
				
			}
		});
    }
    
    public String laydulieu(int result) {
    	final SharedPreferences mSp = getSharedPreferences(END_CLASS,MODE_PRIVATE);
		final String str_result = mSp.getString("Data"+result, "");
	return str_result;
}
    public void cleardulieu() {
    	 SharedPreferences mSp = getSharedPreferences(END_CLASS,MODE_PRIVATE);
    	 SharedPreferences.Editor editor = mSp.edit();
    	 editor.clear();
    	 editor.commit();
    	 num =0;
    	 count =0;
    	 LinearLayout ll = (LinearLayout)findViewById(R.id.root);
		 ll.removeAllViews();
}

	public int  tinhtoan(int count) {
    	Random mrandom = new Random();
			int randomnum;
			randomnum= (mrandom.nextInt(count))+1;
			Log.i(LOG_TAG, "Random : "+randomnum);
			 return randomnum;
	
}

	public Builder createcustomdialog() {
		AlertDialog.Builder alert = new AlertDialog.Builder(this);
		final View dialog_v =getLayoutInflater().inflate(R.layout.dialog_layout, null);
		alert.setView(dialog_v);
		alert.setTitle("Thêm dữ liệu");
		alert.setMessage("Dữ liệu cần tính toán");
		alert.setPositiveButton("Thêm", new DialogInterface.OnClickListener() {

			
		

			public void onClick(DialogInterface dialog, int whichButton) {

				EditText inputdata = (EditText)dialog_v.findViewById(R.id.editText1);
				Editable data = inputdata.getText();
				LinearLayout ll = (LinearLayout)findViewById(R.id.root);
				TextView mdata = new TextView(getApplicationContext());
				num++;
				mdata.setTextSize(20);
				Random myColor = new Random();
				mdata.setTextColor(Color.rgb(myColor.nextInt(255), myColor.nextInt(255), myColor.nextInt(255)));
				mdata.setText(num+": "+data);
				SharedPreferences mSp = getSharedPreferences(END_CLASS,MODE_PRIVATE);
				SharedPreferences.Editor editor = mSp.edit();
				editor.putString("Data"+num, data.toString());
				editor.commit();
				ll.addView(mdata);
				count++;
				
				Log.i(LOG_TAG, ""+num+"  "+count);
			}
		});

		alert.setNegativeButton("Hủy", new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int whichButton) {

			}
			
		});
		return alert;
	}

	@Override
	protected void onApplyThemeResource(Theme theme, int resid, boolean first) {
		// TODO Auto-generated method stub
		super.onApplyThemeResource(theme, resid, first);
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		Log.i(LOG_TAG, "On Config Change");
		super.onConfigurationChanged(newConfig);
	}



}